﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MiniBug.Authentication;

namespace MiniBug.Controllers
{
    public class UserController : Controller
    {

        private Entity.MSSQLEntity dbContext = new Entity.MSSQLEntity();
        //
        // GET: /User/

        [Authorize]
        public ActionResult Index()
        {
            return View(dbContext.User.ToList());
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        [Authorize]
        public ActionResult Settings()
        {
            return View();
        }

        [Authorize]
        public ActionResult SettingsEditUser()
        {
            string login = HttpContext.User.Identity.Name;
            var result = (from p in dbContext.User
                          where p.Login == login
                          select p).FirstOrDefault();
            return RedirectToAction("Edit/" + result.idUser);
        }

        //
        // GET: /User/Create

        [Authorize]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /User/Create

        [HttpPost]
        [Authorize]
        public ActionResult Create([Bind(Exclude = "idUser")] Entity.User newUser)
        {
            try
            {
                string password = CryptographProvider.Encrypt(newUser.Password, newUser.Name);
                newUser.Password = password;
                dbContext.AddToUser(newUser);
                dbContext.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
      
        //
        // GET: /User/Edit/5
 
        [Authorize]
        public ActionResult Edit(int id)
        {
            var result = (from p in dbContext.User
                          where p.idUser == id
                          select p).FirstOrDefault();
            return View(result);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        [Authorize]
        public ActionResult Edit(Entity.User userToEdit)
        {
            try
            {
                var originalUser= (from p in dbContext.User
                                       where p.idUser == userToEdit.idUser
                                       select p).FirstOrDefault();

                string password = CryptographProvider.Encrypt(userToEdit.Password, userToEdit.Login);
                userToEdit.Password = password;

                dbContext.ApplyCurrentValues(originalUser.EntityKey.EntitySetName,userToEdit);
                dbContext.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Delete/5
 
        [Authorize]
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /User/Delete/5

        [HttpPost]
        [Authorize]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }      

    }
}
